package com.example.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.domain.Love;
import com.example.blog.domain.Response;
import com.example.blog.dto.love.AdminSelectLoveDto;
import com.example.blog.note.LogInfo;
import com.example.blog.note.NoToken;
import com.example.blog.note.Permissions;
import com.example.blog.service.impl.LoveServiceImpl;
import com.example.blog.utils.IpUtil;
import com.example.blog.dto.love.LoveInsertDto;
import com.example.blog.dto.publicDto.QuerySearchDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@RestController
@Transactional
@RequestMapping("/love")
public class LoveController extends BaseController{
    private final LoveServiceImpl loveService;
    @Autowired
    public LoveController( LoveServiceImpl loveService){
        this.loveService = loveService;
    }
    //获取所有祝福评论
    @NoToken
    @LogInfo("获取所有祝福评论")
    @GetMapping("/getAll/{index}")
    public Response getAll(@PathVariable("index") Integer index){
        IPage page = new Page(index,7);
        LambdaQueryWrapper<Love> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Love::getTime);
        List<Object> list = new ArrayList<>();
        list.add(loveService.page(page,wrapper).getRecords());
        list.add( page.getPages());
        return new Response(Response.SUCCEED_CODE,"获取所有祝福评论",list);
    }
    //新增祝福评论
    @NoToken
    @LogInfo("新增祝福评论")
    @PostMapping("/insert")
    public Response insert(@RequestBody LoveInsertDto loveVo){
        Love love = new Love(loveVo.getUrl(), loveVo.getContent(),IpUtil.getIpAddress(getHttpInfo()));
        return new Response(Response.SUCCEED_CODE,"新增祝福评论",loveService.save(love));
    }

    //删除祝福评论
    @LogInfo("删除祝福评论(后台管理)")
    @Permissions("/love/delete")
    @DeleteMapping("/delete/{id}")
    public Response delete(@PathVariable("id")long id ){
        LambdaQueryWrapper<Love> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Love::getId,id);
        return new Response(Response.SUCCEED_CODE,"删除成功",loveService.remove(wrapper));
    }

    //获取所有留言信息(后台管理)
    @Permissions("/love/getAll/admin")
    @LogInfo("获取祝福留言信息(后台管理)")
    @PostMapping("/getAll/admin")
    public Response getAllAdmin( @RequestBody AdminSelectLoveDto querySearchDto){
        IPage page = new Page(querySearchDto.getIndex(),querySearchDto.getPage());
        LambdaQueryWrapper<Love> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(querySearchDto.getDateTime()),Love::getTime,querySearchDto.getDateTime())
                .orderByDesc(Love::getTime);
        Optional.ofNullable(querySearchDto.getCondition())
                .ifPresent(s->{
                    queryWrapper.and(wrapper->wrapper
                            .like(Love::getUrl,querySearchDto.getCondition())
                            .or()
                            .like(Love::getContent,querySearchDto.getCondition()));
                          });
        return new Response(Response.SUCCEED_CODE,"获取成功",loveService.page(page,queryWrapper));
    }
}
